package web.servlet.worker;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;

import service.WorkerServiceImpl;
import util.UUIDUtil;
import domain.Worker;

public class WorkerRegistServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Worker worker = new Worker();
		try {
			BeanUtils.populate(worker, request.getParameterMap());
			worker.setWorkerId(UUIDUtil.getUUID());//手动生成一个随机ID
		} catch (Exception e) {
			e.printStackTrace();
		}
		WorkerServiceImpl ws = new WorkerServiceImpl();
		HttpSession session = request.getSession();
		if(ws.regist(worker)){
			session.setAttribute("massage", "恭喜你，注册成功！！！，您现在可以登录了！");
			response.sendRedirect("/PetsManagement/login.jsp");
		}else{
			session.setAttribute("massage", "注册失败，账号已被注册！请换一个账号注册！");
			response.sendRedirect("/PetsManagement/register.jsp");
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
